﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace RentIt06
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "CreateAccount" in code, svc and config file together.
    public class CreateAccountSession : ICreateAccountSession
    {
        public void CreateAccount(string email, string password)
        {
            if (string.IsNullOrEmpty(password)) throw new FaultException("Password must not be empty");
            if (string.IsNullOrEmpty(email)) throw new FaultException("Email must not be empty");
            try {
                Log.Write("Creating account: " + email);
                using (var map = new db.DataMapDataContext())
                {
                    var user = new db.User {
                        email = email,
                        password = Account.hash(password, email),
                        role = "User",
                        firstName = "",
                        lastname = "",
                        country = "  ",
                        address = "",
                        zipCode = "",
                        city = "",
                        phoneNumber = "",
                        credit = 0
                    };
                    map.Users.InsertOnSubmit(user);
                    map.SubmitChanges();
                }
            } catch (System.Data.SqlClient.SqlException) {
                throw new FaultException("Account already exists");
            } catch (Exception e) {
                Log.Write(e);
                throw new FaultException("Unexpected Exception");
            }
        }
    }
}
